using Photon.Pun;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEditor.Progress;
using HashTable = ExitGames.Client.Photon.Hashtable;

public class CharSelectView : MonoBehaviourPunCallbacks
{
    public ListView charPool;
    public ListView myCharSelected;

    public GameObject itemPrefab;

    public Transform poolItemRoot;
    public Transform myItemRoot;

    public CharItem selectedPoolItem;   // 当前被选中的卡池中的item
    public CharItem selectedMyItem;   // 当前被选中的玩家手牌中的item

    #region 网络相关
    HashTable OnSelectTable = new HashTable();
    private bool isSelected = false;
    public bool IsSelected
    {
        get { return isSelected; }
        set 
        { 
            isSelected = value;
            if (isSelected == true)
            {
                ReFreshUI();
            }

        }
    }

    public override void OnPlayerPropertiesUpdate(Photon.Realtime.Player targetPlayer, HashTable changedProps)
    {
        if (targetPlayer != PhotonNetwork.LocalPlayer)
        {
            isSelected = (bool)changedProps["IsSelected"];
        }
    }
    #endregion


    private void Start()
    {
        OnSelectTable.Add("IsSelected", isSelected);
        charPool.onItemSelected += this.OnPoolCharSelected;
        myCharSelected.onItemSelected += this.OnMyCharSelected;
        ReFreshUI();
    }

    private void OnPoolCharSelected(ListView.ListViewItem item)
    {
        this.selectedPoolItem = item as CharItem;
    }
    private void OnMyCharSelected(ListView.ListViewItem item)
    {
        this.selectedMyItem = item as CharItem;
        // TODO:显示当前选中角色详细信息面板
    }

    /// <summary>
    /// 玩家点击了选择武将按钮
    /// </summary>
    public void OnClickSelectButton()
    {
        CharacterSelectManager.Instance.charPlayerCanSelect--;   // 玩家可选角色数量-1
        CharacterSelectManager.Instance.currCharacterCount--;   //卡池剩余角色数量-1

        isSelected = true;
        OnSelectTable["IsSelected"] = isSelected;
        PhotonNetwork.LocalPlayer.SetCustomProperties(OnSelectTable);   // 通知对方我按下了选择按钮

        if (PhotonNetwork.IsMasterClient)
        {
            GameManager.Instance.red_CharCards.Add(selectedPoolItem.cardDefine);    // 角色保存在红方
            GameManager.Instance.Pool_CharCards.Remove(selectedPoolItem.cardDefine);    // 卡池中去掉该角色
        }
        else
        {
            GameManager.Instance.blue_CharCards.Add(selectedPoolItem.cardDefine);
            GameManager.Instance.Pool_CharCards.Remove(selectedPoolItem.cardDefine);
        }

        ReFreshUI();
    }


    #region 刷新UI
    public void ReFreshUI()
    {
        isSelected = false;     // 将选择状态调回false
        ClearPoolCharList();
        ClearMyCharList();
        InitMyItems();
        InitPoolItems();
    }

    #region 清空listView
    private void ClearPoolCharList()
    {
        this.charPool.RemoveAll();
    }
    private void ClearMyCharList()
    {
        this.myCharSelected.RemoveAll();
    }
    #endregion


    #region 初始化listView
    private void InitMyItems()
    {
        if (PhotonNetwork.IsMasterClient)
        {
            foreach (var item in GameManager.Instance.red_CharCards)
            {
                GameObject go = Instantiate(itemPrefab, myItemRoot);  // 生成预制体

                CharItem charItem = go.GetComponent<CharItem>();    // 赋值
                charItem.SetCharInfo(item);
                this.myCharSelected.AddItem(charItem);
            }
        }
        else
        {
            foreach (var item in GameManager.Instance.blue_CharCards)
            {
                GameObject go = Instantiate(itemPrefab, myItemRoot);  // 生成预制体

                CharItem charItem = go.GetComponent<CharItem>();    // 赋值
                charItem.SetCharInfo(item);
                this.myCharSelected.AddItem(charItem);
            }
        }
    }

    private void InitPoolItems()
    {
        for (int i = 0; i < GameManager.Instance.Pool_CharCards.Count; i++)
        {
            GameObject go = Instantiate(itemPrefab, poolItemRoot);  // 生成预制体

            CharItem charItem = go.GetComponent<CharItem>();    // 赋值
            charItem.SetCharInfo(GameManager.Instance.Pool_CharCards[i]);
            this.myCharSelected.AddItem(charItem);
        }
    }
    #endregion

    #endregion

}
